/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.source.controller;

import com.wisnu.spms.source.model.PanelLoginModel;
import com.wisnu.spms.source.view.PanelLogin;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Wisnu Wardoyo
 */
public class PanelLoginController {

    PanelLoginModel model;
    MainFrameController mainFrameController;

    public PanelLoginController(PanelLoginModel model, MainFrameController mainFrameController) {
        this.model = model;
        this.mainFrameController = mainFrameController;
    }

    public void login(PanelLogin login) {
        String username = login.getTxtUsername().getText();
        String password = new String(login.getTxtPassword().getPassword());

        if (username.trim().equals("")) {
            JOptionPane.showMessageDialog(login, "Username masih kosong");
        } else if (username.length() > 25) {
            JOptionPane.showMessageDialog(login, "Username lebih dari 25 karakter");
        } else if (password.trim().equals("")) {
            JOptionPane.showMessageDialog(login, "Password masih kosong");
        } else if (password.length() > 50) {
            JOptionPane.showMessageDialog(login, "Password lebih dari 50 karakter");
        } else {
            try {
                model.setUsername(username);
                model.setPassword(password);
                if (model.isLogin()) {
                    JOptionPane.showMessageDialog(login, "yey i'm loged in");
                    model.updateEmployee();
                    mainFrameController.showAdminPanel();
                } else {
                    JOptionPane.showMessageDialog(login, "Something wrong");
                    model.setUsername("");
                    model.setPassword("");
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(login, ex.getMessage());
            }
        }
    }

}
